﻿using System.Web.Security;
using System.Web;
using System;
//using msBsc.OnlineUser;
//using yaf;

namespace msBsc.SvRef
{
    /// <summary>
    /// UserInfo 的摘要说明
    /// </summary>
    public class UserInfo
    {


        public UserInfo()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        //--<option value="0">普通职员</option>
        //--<option value="1">小组长</option>
        //--<option value="2">部门副经理</option>
        //--<option value="3">部门经理</option>
        //--<option value="4">公司副总经理</option>
        //--<option value="5">公司总经理</option>
        //--<option value="6">集团公司副总经理</option>
        //--<option value="7">集团公司总经理</option> 
        public static string OperRank
        {
            get
            {
                return msBsc.AppLogic.Cfg.BseUserInfo.OperRank;
            }
        }

        //public string operRole
        //{
        //    get
        //    {
        //        return Epm.AppLogic.Framework.ApplicationInfo.operRole;
        //    }
        //    set
        //    {
        //        Epm.AppLogic.Framework.ApplicationInfo.operRole = value;
        //    }
        //}

        /// <summary>
        /// 店铺ID
        /// </summary>
        public static string OperDealerId
        {
            get
            {
                if (string.IsNullOrEmpty(Operator))
                {
                    return "";
                }
                if (System.Web.HttpContext.Current == null || System.Web.HttpContext.Current.Session == null || System.Web.HttpContext.Current.Session["choperdealerid"] == null || System.Web.HttpContext.Current.Session["choperdealerid"] != null && (string)System.Web.HttpContext.Current.Session["choperdealerid"] == string.Empty)
                {
                    using (Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
                    {
                        if (Operator != "")
                        {
                            string strSql = "SELECT COMM_TENANT_ID,COMM_TENANT_NME,COMM_TENANT_SID FROM SHP_COMMERCIAL_TENANT_INFO WHERE USER_NME='" + Operator + "'";

                            System.Data.DataSet ds = oCDbAccess.QueryBySQL(strSql);
                            string strret = "";
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                strret = ds.Tables[0].Rows[0]["COMM_TENANT_ID"].ToString() + "," + ds.Tables[0].Rows[0]["COMM_TENANT_NME"].ToString() + "," + ds.Tables[0].Rows[0]["COMM_TENANT_SID"].ToString();
                            }

                            if (System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.Session != null)
                            {
                                System.Web.HttpContext.Current.Session["choperdealerid"] = strret;
                            }
                            return strret;
                        }
                    }
                    return "";
                }
                else
                {
                    return (string)System.Web.HttpContext.Current.Session["choperdealerid"];
                }
            }
            set
            {
                System.Web.HttpContext.Current.Session["choperdealerid"] = value;
            }
        }

        public static string OperType
        {
            get
            {
                //if (HttpContext.Current == null || HttpContext.Current.Session == null)
                //{
                //    return "";
                //}
                return msBsc.AppLogic.Cfg.BseUserInfo.OperType;
            }
        }

        public static string Operator
        {
            get
            {
                //if (HttpContext.Current == null || HttpContext.Current.Session == null)
                //{
                //    return "";
                //}
                return msBsc.AppLogic.Cfg.BseUserInfo.Operator;
            }
        }

        public static string OperatorName
        {
            get
            {
                return msBsc.AppLogic.Cfg.BseUserInfo.OperatorName;
            }
        }


        public static string GetParamValue(string strCallBackParam, string strParam)
        {
            if (string.IsNullOrEmpty(strCallBackParam))
                return string.Empty;
            else
            {
                string[] arrParam = strCallBackParam.Split(new char[] { ';' });
                for (int i = 0; i < arrParam.Length; i++)
                {
                    string[] arrValue = arrParam[i].Split(new char[] { '|' });
                    if (arrValue.Length > 0)
                    {
                        if (arrValue[0].Trim().ToUpper() == strParam.Trim().ToUpper())
                        {
                            if (arrValue.Length > 1)
                                return arrValue[1];
                            else
                                return string.Empty;
                        }
                    }
                }
            }
            return string.Empty;
        }


        public static bool Online
        {
            get
            {
                return msBsc.AppLogic.Cfg.BseUserInfo.Operator != "";
            }
        }

        public static bool UsrIsCmfInnerPwd { get; set; }


        public static string ChangePass(string strOldPass, string strNewPass, out bool blSuccess)
        {
            blSuccess = false;
            if (!Online)
            {
                return "用户未登录！";
            }
            using (Epm.AppLogic.Framework.CDbAccess objtem1 = new Epm.AppLogic.Framework.CDbAccess())
            {
                System.Data.DataSet ds = objtem1.QueryBySQL("SELECT  EMPLOYEE_INFO_PWD  FROM DJK_EMPLOYEE_INFO WHERE  EMPLOYEE_INFO_CODE='" + Operator + "'");
                if (ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0]["EMPLOYEE_INFO_PWD"].ToString() == msBsc.AppLogic.Cfg.DesCry.encMe(strOldPass, "zbhpass"))
                {
                    objtem1.ExecuteBySQL("UPDATE DJK_EMPLOYEE_INFO SET EMPLOYEE_INFO_PWD='" + msBsc.AppLogic.Cfg.DesCry.encMe(strNewPass, "zbhpass") + "' WHERE  EMPLOYEE_INFO_CODE='" + Operator + "'");
                    blSuccess = true;
                    return "OK";
                }
            }
            return "用户信息未找到或原密码错误";
            //}

        }

        public static string ChangeAgentPass(string strOldPass, string strNewPass, out bool blSuccess)
        {
            blSuccess = false;
            if (!Online)
            {
                return "用户未登录！";
            }
            using (Epm.AppLogic.Framework.CDbAccess objtem1 = new Epm.AppLogic.Framework.CDbAccess())
            {
                System.Data.DataSet ds = objtem1.QueryBySQL("SELECT  CUSTOMER_REGISTER_PWD  FROM DJK_CUSTOMER_REGISTER WHERE  CUSTOMER_REGISTER_ID='" + Operator + "'");
                if (ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0]["CUSTOMER_REGISTER_PWD"].ToString() == msBsc.AppLogic.Cfg.DesCry.encMe(strOldPass, "zbhpass"))
                {
                    objtem1.ExecuteBySQL("UPDATE DJK_CUSTOMER_REGISTER SET CUSTOMER_REGISTER_PWD='" + msBsc.AppLogic.Cfg.DesCry.encMe(strNewPass, "zbhpass") + "' WHERE  CUSTOMER_REGISTER_ID='" + Operator + "'");
                    blSuccess = true;
                    return "OK";
                }
            }
            return "用户信息未找到或原密码错误";

        }

        private static string[] _strInfo = new string[] { "", "" };
        public static string[] GetOperateInfo()
        {
            if (Online)
            {
                if (_strInfo[0] == string.Empty)
                {
                    using (Epm.AppLogic.Framework.CDbAccess objtem1 = new Epm.AppLogic.Framework.CDbAccess())
                    {
                        System.Data.DataSet ds = objtem1.QueryBySQL("SELECT E_MAIL,TEL,LOGIN_PSWD,USER_TYPE_CID,USER_RANK FROM SYS_USER WHERE REC_ACTIVE =1 AND  USER_NME='" + Operator + "'");
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            _strInfo[0] = ds.Tables[0].Rows[0]["E_MAIL"].ToString();
                            _strInfo[1] = ds.Tables[0].Rows[0]["TEL"].ToString();
                        }
                    }

                }
            }
            return _strInfo;
        }

        #region 登录
        //用户登录
        public static string LoginCustomer(string strUser, string strPass)
        {
            strUser = strUser.Replace("'", "").Replace("|", "");

            using (Epm.AppLogic.Framework.CDbAccess oacc = new Epm.AppLogic.Framework.CDbAccess())
            {
                Epm.AppLogic.Framework.CDbParmCollection parmCollection = new Epm.AppLogic.Framework.CDbParmCollection();
                parmCollection.Add(new Epm.AppLogic.Framework.CDbParm("strUserNo", System.Data.ParameterDirection.Input, Epm.AppLogic.Framework.EDbType.String, 36, strUser));

                System.Data.DataSet ds = oacc.QueryBySQL(@"SELECT CUSTOMER_REGISTER_ID,
    CUSTOMER_REGISTER_PWD,CUSTOMER_NICK,CUSTOMER_ACCOUNT_TYPE,PWD_IS_MODIFIED FROM DJK_CUSTOMER_REGISTER  WHERE ACCOUNT_IS_DELETED=0 AND CUSTOMER_REGISTER_ACCOUNT=@strUserNo", parmCollection);

                if (ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0]["CUSTOMER_REGISTER_PWD"].ToString() == msBsc.AppLogic.Cfg.DesCry.encMe(strPass, "zbhpass"))
                {
                    //_strInfo[0] = ds.Tables[0].Rows[0]["E_MAIL"].ToString();
                    //_strInfo[1] = ds.Tables[0].Rows[0]["TEL"].ToString();
                    doSetAuthCookie(ds.Tables[0].Rows[0]["CUSTOMER_REGISTER_ID"].ToString(), ds.Tables[0].Rows[0]["CUSTOMER_ACCOUNT_TYPE"].ToString(), "", ds.Tables[0].Rows[0]["CUSTOMER_NICK"].ToString());
                    //objtem1.ExecuteBySQL("UPDATE SYS_USER SET LAST_LOGIN_DT=GETDATE()  WHERE USER_NME='" + strUser + "'");
                    bool isModifyPwd = false;

                    if (ds.Tables[0].Rows[0]["CUSTOMER_ACCOUNT_TYPE"].ToString() == "3" 
                        && !bool.TryParse(ds.Tables[0].Rows[0]["PWD_IS_MODIFIED"].ToString(), out isModifyPwd))
                    {
                        isModifyPwd = false;
                    }

                    return isModifyPwd ? "OK" : "OK1";

                }
            }
            return "用户名或密码错误！";

        }
        //员工登录
        public static string Login(string strUser, string strPass)
        {
            strUser = strUser.Replace("'", "").Replace("|", "");

            using (Epm.AppLogic.Framework.CDbAccess oacc = new Epm.AppLogic.Framework.CDbAccess())
            {
                Epm.AppLogic.Framework.CDbParmCollection parmCollection = new Epm.AppLogic.Framework.CDbParmCollection();
                parmCollection.Add(new Epm.AppLogic.Framework.CDbParm("strUserNo", System.Data.ParameterDirection.Input, Epm.AppLogic.Framework.EDbType.String, 36, strUser));

                System.Data.DataSet ds = oacc.QueryBySQL(@"SELECT EMPLOYEE_INFO_CODE,EMPLOYEE_INFO_ID,EMPLOYEE_INFO_NAME,EMPLOYEE_INFO_PWD
        FROM DJK_EMPLOYEE_INFO 
        WHERE EMPLOYEE_IS_DELETED=0 AND EMPLOYEE_IS_DISPLAY=1 AND EMPLOYEE_INFO_CODE=@strUserNo", parmCollection);

                if (ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0]["EMPLOYEE_INFO_PWD"].ToString() == msBsc.AppLogic.Cfg.DesCry.encMe(strPass, "zbhpass"))
                {
                    //_strInfo[0] = ds.Tables[0].Rows[0]["E_MAIL"].ToString();
                    //_strInfo[1] = ds.Tables[0].Rows[0]["TEL"].ToString();
                    doSetAuthCookie(ds.Tables[0].Rows[0]["EMPLOYEE_INFO_CODE"].ToString(), "0", "", ds.Tables[0].Rows[0]["EMPLOYEE_INFO_ID"].ToString());
                    //objtem1.ExecuteBySQL("UPDATE SYS_USER SET LAST_LOGIN_DT=GETDATE()  WHERE USER_NME='" + strUser + "'");
                    return "OK";
                }
            }
            return "用户名或密码错误！";

        }
        #endregion

        public static void doSetAuthCookie(string strpUserCde, string strpoperType, string strpoperRank, string strUserNme)
        {
            msBsc.AppLogic.Cfg.BseUserInfo.doSetAuthCookie(strpUserCde, strpoperType, strpoperRank, strUserNme);
        }

        public static void ExitSys()
        {
            msBsc.AppLogic.Cfg.BseUserInfo.ExitSys();
        }


    }
    //5_1_a_s_p_x.c_o_m
}